import Vue from "vue";
import VueRouter from "vue-router";
import login from "@/components/login";
import home from "@/views/home";
import welcome from "@/views/welcome";
import users from "@/views/user/users";
import rights from "@/views/power/rights";
import roles from "@/views/power/roles";
import Cate from "@/views/goods/Cate";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: login,
  },
  {
    path: "/home",
    component: home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: welcome },
      { path: "/users", component: users },
      { path: "/rights", component: rights },
      { path: "/roles", component: roles },
      { path: "/categories", component: Cate },
    ],
  },
];

const router = new VueRouter({
  routes,
  mode: "history",
});

//前置钩子
router.beforeEach((to, from, next) => {
  //从from跳转到to
  //from代表从哪个路径跳转过来
  //to将要访问的路径
  //next()放行
  if (to.path === "/login") {
    return next();
  }
  const tokenStr = window.sessionStorage.getItem("token");
  if (tokenStr == null) {
    return next("/login");
  }
  next();
});
export default router;
